package ru.tomsk.pear.remotecontrolserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ServerSide extends Thread {
    private Socket socket = null;

    public ServerSide(Socket socket) {
    	super("ServerSideThread");
    	this.socket = socket;
    }

    public void run() {
		try {
		    BufferedReader in = new BufferedReader(
					    new InputStreamReader(
					    socket.getInputStream()));
	
		    String inputLine;
	
		    while ((inputLine = in.readLine()) != null) {
		    	String[] cmd = { "/usr/local/bin/bash", "-c", inputLine };
		    	System.out.println("Got signal {" + inputLine + "} !");
		        Process p = Runtime.getRuntime().exec(/*"bash " + */cmd/*inputLine*/);
		        p.waitFor();
		        System.out.println("Exit code: " + p.exitValue());
		        Thread.sleep(1000);
		    }
		    in.close();
		    socket.close();
	
		} catch (IOException e) {
		    e.printStackTrace();
		} catch (Exception err) {
			
		}
    }
}
